package main

import (
	"fmt"
)

func main() {
	h1 := &ListNode{0, nil}
	h2 := &ListNode{7, nil}
	// l1 := h1
	l2 := h2

	// l1.Next = &ListNode{8, nil}
	l2.Next = &ListNode{3, nil}
	// l1 = l1.Next
	l2 = l2.Next

	// l1.Next = &ListNode{3, nil}

	// l1 = l1.Next

	res := addTwoNumbers(h1, h2)
	for res != nil {
		fmt.Println(res.Val)
		res = res.Next
	}
}

type ListNode struct {
	Val  int
	Next *ListNode
}

func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
	if l1 == nil && l2 == nil {
		return nil
	}
	head := &ListNode{0, nil}
	point := head
	carry := 0
	sum := 0
	for l1 != nil && l2 != nil {
		sum = l1.Val + l2.Val + carry
		point.Next = &ListNode{sum % 10, nil}
		carry = sum / 10
		point = point.Next
		l1 = l1.Next
		l2 = l2.Next
	}

	for l1 != nil {
		sum = l1.Val + carry
		point.Next = &ListNode{sum % 10, nil}
		carry = sum / 10
		point = point.Next
		l1 = l1.Next
	}

	for l2 != nil {
		sum = l2.Val + carry
		point.Next = &ListNode{sum % 10, nil}
		carry = sum / 10
		point = point.Next
		l2 = l2.Next
	}

	if carry != 0 {
		point.Next = &ListNode{carry, nil}
	}

	return head.Next

}
